<?php

class Orders extends CI_Controller {
    
    private $_auth;
    public function __construct() {
        parent::__construct();
        $this->_auth = $this->myclass->checkuser();
        if($this->_auth == FALSE) redirect ("backend/home");
    }
    
    public function index(){
        $config['first_link']='Đầu';
        $config['last_link'] = 'Cuối';
        $config['cur_tag_open'] = "<a class='selected'>";
        $config['cur_tag_close'] = "</a>";
        $config['full_tag_open'] = "<div class='paging'>";
        $config['full_tag_close'] = '</div';
        
        $this->load->library('pagination');
        $config['uri_segment'] = 4;
        $config['base_url'] = base_url() . 'backend/orders/index';
        $config['total_rows'] = count($this->morders->fetchAll());
        $config['per_page'] = 9;
        $this->pagination->initialize($config);
        $data['data']['orders'] = $this->morders->fetchAll($config['per_page'], $this->uri->segment(4));
        $data['data']['paging'] = $this->pagination->create_links();

        $data['title'] = "Bảng đơn hàng";
        $data['temp'] = "backend/orders/home";
        $this->load->view("backend/layout/index", $data);
    }
    
    
    public function cancel($id){
        $o = $this->morders->fetchOne($id);
        if($o->status == 0){
        $pro = $this->morders_detail->getByOrderId($id);
        foreach($pro as $p){
            $this->mproducts->updateQty2($p->pro_id, $p->qty);
        }
        $this->morders->delete($id);
        $this->morders_detail->delete($id);
        redirect("backend/orders");
        } else {
            $this->myclass->js_redirect("Đơn hàng đã xử lý không thể xóa", base_url("backend/orders"));
        }       
    }
    
    
    public function update($id){
        if($this->input->post("add")){
            $post = $this->input->post("data");
        }
        $this->form_validation->set_error_delimiters('<li>', '</li>');       
        $this->form_validation->set_rules('data[address]', 'Địa chỉ', 'trim|required');
        $this->form_validation->set_rules('data[req]', 'Tên', 'trim');
        $this->form_validation->set_rules('data[deli_date]', 'Ngày giao hàng', 'trim|required');
        if ($this->form_validation->run() == TRUE){
            $this->morders->update($id,$post);
            redirect("backend/orders");
        }
        $data['data']['cat']= $this->morders->fetchOne($id);
        $data['title'] = "Sửa danh mục";
        $data['temp'] = "backend/orders/form";
        $this->load->view("backend/layout/index",$data);
    }
    
    public function stat(){
        $data['data']['orders']= $this->morders->getStatByDay();
        $data['title'] = "Thống kê theo ngày";
        $data['temp'] = "backend/orders/list";
        $this->load->view("backend/layout/index",$data);
    }
    
    public function statM(){
        $data['data']['orders']= $this->morders->getStatByMonth();
        $data['title'] = "Thống kê theo tháng";
        $data['temp'] = "backend/orders/list_1";
        $this->load->view("backend/layout/index",$data);
    }
}
